package com.sky.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sky.dto.CategoryDTO;
import com.sky.entity.Category;
import com.sky.result.Result;
import com.sky.service.admin.CategoryService;
import com.sky.utils.ChangeType;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.Cacheable;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/admin/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 修改分类
     *
     * @param categoryDTO
     * @return
     */
    @PutMapping
    Result<Object> modifyCategory(@RequestBody CategoryDTO categoryDTO) {
        Category category1 = new Category();
        Category category = ChangeType.change(categoryDTO, category1);
        log.info("修改分类：{}", category);
        categoryService.updateById(category);
        return Result.success();
    }


    /**
     * 分类分页查询
     *
     * @param name
     * @param page
     * @param pageSize
     * @param type
     * @return
     */
    @GetMapping("/page")
    Result<IPage<Category>> findAllPage(@RequestParam(required = false) String name,
                                        @RequestParam Integer page,
                                        @RequestParam Integer pageSize,
                                        @RequestParam(required = false) Integer type) {
//        Page<Category> categoryPage = new Page<>();
//        categoryPage.setSize(pageSize);
//        categoryPage.setCurrent(page);
//
//        QueryWrapper<Category> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq(type != null, "type", type)
//                .like(name != null, "name", name);

//        IPage<Category> pageResult = categoryService.page(categoryPage, queryWrapper);
        IPage<Category> pageResult = categoryService.myPage(name,page,pageSize,type);
        return Result.success(pageResult);
    }

    /**
     * 启用禁用分类
     *
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    public Result<Object> enableOrDisabled(@PathVariable Integer status, @RequestParam Integer id) {
//        Category category = categoryService.getById(id);
//        category.setStatus(status);
//        categoryService.updateById(category);
        categoryService.myUpdateById(id,status);
        return Result.success();
    }

    /**
     * 新增分类
     *
     * @param categoryDTO
     * @return
     */
    @PostMapping
    public Result<Object> add(@RequestBody CategoryDTO categoryDTO) {
//        Category categoryType = new Category();
//        Category category = ChangeType.change(categoryDTO, categoryType);
        categoryService.addByObject(categoryDTO);
        return Result.success();
    }


    /**
     * 删除分类
     *
     * @param id
     * @return
     */
    @DeleteMapping
    public Result<Object> deleteById(@RequestParam Integer id) {
        categoryService.deleteById(id);
        return Result.success();
    }

    /**
     * 根据类型查询分类
     *
     * @param type
     * @return
     */
    @GetMapping("/list")
    public Result<Object> selectCategoryByType(@RequestParam Integer type) {
        long l = System.currentTimeMillis();
        List<Category> list = categoryService.getByType(type);
        log.error("查询耗时：{}ms", System.currentTimeMillis() - l);
        return Result.success(list);
    }

}
